package com.talent.service.iot.mqtt.handler;

import com.talent.service.iot.mqtt.config.Constant;
import com.talent.service.iot.mqtt.model.request.TlMqttPubCompReq;
import com.talent.service.iot.mqtt.model.variable.TlMqttPubCompVariableHead;
import com.talent.service.iot.mqtt.service.IMqttMsgService;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.AttributeKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @ClassName：ConnectHandler
 * @Author: hszhou
 * @Date: 2024/11/25 11:06
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@ChannelHandler.Sharable
@Component
public class TlMqttPubCompHandler extends SimpleChannelInboundHandler<TlMqttPubCompReq> {

    private static final  Logger logger = LoggerFactory.getLogger(TlMqttPubCompHandler.class);

    private final IMqttMsgService msgService;


    TlMqttPubCompHandler(IMqttMsgService msgService){
        this.msgService = msgService;
    }


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TlMqttPubCompReq msg) throws Exception {

        logger.debug("in【PUBCOMP】 handler");


        Object clientId = ctx.channel().attr(AttributeKey.valueOf(Constant.CLIENT_ID)).get();
        if(clientId==null){
            ctx.channel().close();
            return;
        }
        TlMqttPubCompVariableHead variableHead = msg.getVariableHead();

        Integer messageId = variableHead.getMessageId();
        logger.debug("receive client 【{}】 messageId 【{}】 COMP",clientId,messageId);
        //删除rec消息
        msgService.clearPubRecMessage(clientId.toString(),messageId);
        logger.debug("【CLIENT】4.【{}】 remove rec message 【{}】",clientId,messageId);


    }
}
